/*
@title: Tag Your Buddy
@description: "Tag Your Buddy" is a puzzle-solving game where the objective is to reunite two separated characters by navigating the black character through obstacles to reach the red character. Players must avoid blue characters while strategically moving around the map to achieve their goal.
@author: Aadarsh Jonnalagadda
@tags: []
@addedOn: 2024-05-20
@img: ""
*/

const player1 = "p"
const player2 = "t"
const player3 = "e"
const arrowu = "u"
const arrowd ="d"
const arrowr = "r"

setLegend(
  [ player1, bitmap`
................
................
.......00.......
......0..0......
.....0....0.....
.....0....0..0..
.....0.0.00..0..
..0000....0000..
..0..0.0000.....
..0..0....0.....
......0..0......
......0000......
......0..0......
......0..0......
.....00..00.....
................` ],
  [ player2, bitmap`
................
................
.......33.......
......3..3......
.....3....3.....
..3..3....3.....
..3..33.3.3.....
..3333....3333..
.....3333.3..3..
.....3....3..3..
......3..3......
......3333......
......3..3......
......3..3......
.....33..33.....
................`],
  [ player3, bitmap`
................
................
.......55.......
......5..5......
.....5....5.....
5....5....5....5
5....57..75....5
555555....555555
.....577775.....
.....5....5.....
......5..5......
......5555......
......5..5......
......5..5......
.....55..55.....
................`],
  [ arrowu, bitmap`
................
................
................
................
................
................
................
................
........0.......
.......000......
......0.0.0.....
........0.......
........0.......
........0.......
........0.......
................`],
  [ arrowd, bitmap`
................
................
................
................
................
................
................
................
.......0........
.......0........
.......0........
.......0........
.....0.0.0......
......000.......
.......0........
................`],
  [ arrowr, bitmap`
................
................
................
................
................
..........0.....
...........0....
............0...
..000000000000..
............0...
...........0....
..........0.....
................
................
................
................`]
  
);

const levels = [
  map`
urrrrd
p.e..t
......`,
    map`
......
p.e.et
......`,
  map`
..e...
p.e..t
......`,
    map`
......
p.e..t
..e...`,
  map`
...e...eet
.e...e....
.e.e.e.eee
.ee.eee.ee
.ee.eee.ee
p.........`,
    map`
eee........eeeeeee
eee.eeeeee.e...e.t
eee......e.e.e.e.e
eeeeeeee.e.e.e.e.e
e......e.e.e.e.e.e
e.eeee.e.e.e.e.e.e
e....e.e.e.e.e.e.e
eeee.e.e.e.e.e.e.e
eeee.e...e...e...e
p....eeeeeeeeeeeee`,
  map`
e........eeeeeee......eee....
e.eeeeee.eee...e.eeee.eee.eet
e......e.e...e.e...ee...e...e
eeeeee.e.e.eee.eee.eeee.eee.e
e......e.e..ee.e...eeee.e...e
e.eeeeee.ee.e..e.eee....e.eee
e......e....e.ee...e.eeee.eee
eeeeee.eeeeee.eeee.e...ee.eee
e......e......ee...eee.ee.eee
e.eeeeee.eeeeeee.eee...ee.eee
e......e...e...e.eee.eee..eee
eeeeee.eee.e.e.e.ee..eee.eeee
p...ee.eee.e.e...ee.ee...eeee
eee....eee...eeeeee....eeeeee`,
    map`
te.e...e...e...e...e...e...e...e...e...e
.e.e.e.e.e.e.e.e.e.e.e.e.e.e.e.e.e.e.e.e
.e...e...e...e...e...e...e...e...e...e..
.eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee.
.e...e...e...e...e...e...e...e...e...e..
.e.e.e.e.e.e.e.e.e.e.e.e.e.e.e.e.e.e.e.e
.e.e...e...e...e...e...e...e...e...e...e
.e.eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
.e.e...e...e...e...e...e...e...e...e...e
.e.e.e.e.e.e.e.e.e.e.e.e.e.e.e.e.e.e.e.e
.e...e...e...e...e...e...e...e...e...e..
.eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee.
.err.e...e...e...e...e...e...e...e...e..
.eue.e.e.e.e.e.e.e.e.e.e.e.e.e.e.e.e.e.e
.eue...e...e...e...e...e...e...e...e...e
prueeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee`,
  map`
.........................
....eeeeeeeeeeeeeeeeee...
....e.........e...e..e...
....e.eeeeeee.e.e.e..e...
....e....e....e.e.e..e...
eeeeeeee.e.eeee.e.eeeeeee
ep.....e.e....e.e......te
eeeeee.e.eeee.e.eeeeeeeee
...e...e.e....e......e...
...e.eee.e.eeeeeeeee.e...
...e.....e...........e...
...eeeeeeeeeeeeeeeeeee...`,
    map`
e.......................e
e.eeeeeeeeeeeeeeeeeeeee.e
e.e.e.........e...e..ee.e
e.e.e.eeeeeee.e.e.e..ee.e
e.e.e....e....e.e.e..ee.e
e.eeeeee.e.eeee.e.eeeee.e
eprrrr.e.e....e.e.....ete
eeeeee.e.eeee.e.eeeeeeeee
...e...e.e....e......e...
...e.eee.e.eeeeeeeee.e...
...e.....e...........e...
...eeeeeeeeeeeeeeeeeee...`,
  map`
..ee...................e.
..e.eeeeeeeeeeeeeeeeee.e.
..e.e.........e...e..e.e.
..e.e.eeeeeee.e.e.e..e.e.
ee..e....e....e.e.e..e.e.
e..eeeee.e.eeee.e.eeeee.e
ep.....e.e....e.e......te
e.eeee.e.eeee.e.eeeeee.ee
e.ee...e.e....e......eee.
e.ee.eee.e.eeeeeeeee.e.e.
e.ee.....e...........e.e.
e.eeeeeeeeeeeeeeeeeeee.e.
e......................e.`
];


currentLevel = 0
setMap(levels[currentLevel]);

setSolids([ player1, player3]);

onInput("s", () => {
  getFirst(player1).y += 1; 
});

onInput("d", () => {
  getFirst(player1).x += 1;
});

onInput("a", () => {
  getFirst(player1).x -= 1;
});

onInput("w", () => {
  getFirst(player1).y -= 1;
});

onInput("i", () => {
  currentLevel = 0;
  clearText("");
  setMap(levels[currentLevel]);
});

onInput("j", () => {
    clearText("");
    setMap(levels[currentLevel]);
});

afterInput(() => {

  const oneTiles = tilesWith(player1);
  
  const twoTiles = tilesWith(player2);

  if (oneTiles[0][0]._x == twoTiles[0][0]._x && oneTiles[0][0]._y == twoTiles[0][0]._y) {
    currentLevel = currentLevel + 1;

    if (levels[currentLevel] !== undefined) {
      setMap(levels[currentLevel]);
    } else {
      addText("the end", { y: 4, color: color`3` });
    }
  }
});
